$(function() {
    var address = window.location.href
    var search = address.substr(address.indexOf('=') + 1)
    var list = []
    $.ajax({
        url: '/address/queryAddress',
        async: false,
        success: function(res) {
            list = res
        }
    })
    for (var i = 0; i < list.length; i++) {
        if (list[i].id == search) {
            var user = $('#user').val(list[i].recipients)
            var post_code = $('#post_code').val(list[i].postCode)
            var address = $('#address').val(list[i].address)
            var detail_address = $('#detail_address').val(list[i].addressDetail)
            break;
        }
    }


    $('#editBtn').on('tap', function() {
        var user = $('#user').val().trim()
        var post_code = $('#post_code').val().trim()
        var address = $('#address').val().trim()
        var detail_address = $('#detail_address').val().trim()

        if (user.length <= 0) return mui.toast('请输入收货人姓名~');
        if (post_code.length && post_code.length < 6) return mui.toast('邮编不得少于六位~');
        if (address.length < 2) return mui.toast('请输入省市区~');
        if (detail_address.length && detail_address.length < 2) return mui.toast('请输入详细地址~');
        var params = {
            id: search,
            address: address,
            addressDetail: detail_address,
            recipients: user,
            postcode: post_code,
        }
        $.ajax({
            url: '/address/updateAddress',
            type: 'post',
            data: params,
            success: function(res) {
                mui.toast('修改地址成功~');
                setTimeout(function() {
                    window.location.href = "address.html"
                }, 500)
            }
        })
        return false
    })
})